@extends('admin.layouts.app')

@section('title', '组织详情')

@section('content')
<div class="d-flex justify-content-between align-items-center mb-3">
    <h3>组织详情: {{ $organization->name }}</h3>
    <div>
        @can('edit-organizations')
        <a href="{{ route('admin.organizations.edit', $organization) }}" class="btn btn-primary me-2">
            <i class="fas fa-edit me-1"></i>编辑
        </a>
        @endcan
        <a href="{{ route('admin.organizations.index') }}" class="btn btn-secondary">
            <i class="fas fa-arrow-left me-1"></i>返回列表
        </a>
    </div>
</div>

<div class="row">
    <div class="col-md-8">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">基本信息</h5>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <h6 class="text-muted">组织名称</h6>
                        <p class="mb-0">{{ $organization->name }}</p>
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <h6 class="text-muted">组织代码</h6>
                        <p class="mb-0">{{ $organization->code ?: '未设置' }}</p>
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <h6 class="text-muted">上级组织</h6>
                        <p class="mb-0">
                            @if($organization->parent)
                                <a href="{{ route('admin.organizations.show', $organization->parent) }}">
                                    {{ $organization->parent->name }}
                                </a>
                            @else
                                无上级组织
                            @endif
                        </p>
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <h6 class="text-muted">状态</h6>
                        <p class="mb-0">
                            @if($organization->is_active)
                                <span class="badge bg-success">活跃</span>
                            @else
                                <span class="badge bg-danger">禁用</span>
                            @endif
                        </p>
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <h6 class="text-muted">创建时间</h6>
                        <p class="mb-0">{{ $organization->created_at->format('Y-m-d H:i:s') }}</p>
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <h6 class="text-muted">最后更新</h6>
                        <p class="mb-0">{{ $organization->updated_at->format('Y-m-d H:i:s') }}</p>
                    </div>
                </div>
                
                <div class="mb-3">
                    <h6 class="text-muted">组织描述</h6>
                    <p class="mb-0">{{ $organization->description ?: '暂无描述' }}</p>
                </div>
            </div>
        </div>
        
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">联系信息</h5>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <h6 class="text-muted">地址</h6>
                        <p class="mb-0">{{ $organization->address ?: '未设置' }}</p>
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <h6 class="text-muted">联系电话</h6>
                        <p class="mb-0">{{ $organization->phone ?: '未设置' }}</p>
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <h6 class="text-muted">电子邮箱</h6>
                        <p class="mb-0">
                            @if($organization->email)
                                <a href="mailto:{{ $organization->email }}">{{ $organization->email }}</a>
                            @else
                                未设置
                            @endif
                        </p>
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <h6 class="text-muted">网站</h6>
                        <p class="mb-0">
                            @if($organization->website)
                                <a href="{{ $organization->website }}" target="_blank">{{ $organization->website }}</a>
                            @else
                                未设置
                            @endif
                        </p>
                    </div>
                </div>
            </div>
        </div>
        
        @if($organization->children->count() > 0)
        <div class="card mb-4">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0">下级组织</h5>
                <span class="badge bg-primary">{{ $organization->children->count() }}</span>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>名称</th>
                                <th>代码</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($organization->children as $child)
                            <tr>
                                <td>{{ $child->id }}</td>
                                <td>{{ $child->name }}</td>
                                <td>{{ $child->code ?: '-' }}</td>
                                <td>
                                    @if($child->is_active)
                                        <span class="badge bg-success">活跃</span>
                                    @else
                                        <span class="badge bg-danger">禁用</span>
                                    @endif
                                </td>
                                <td>
                                    <a href="{{ route('admin.organizations.show', $child) }}" class="btn btn-sm btn-info">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        @endif
    </div>
    
    <div class="col-md-4">
        @if($organization->logo)
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">组织Logo</h5>
            </div>
            <div class="card-body text-center">
                <img src="{{ asset('storage/' . $organization->logo) }}" alt="{{ $organization->name }} Logo" class="img-fluid" style="max-height: 200px;">
            </div>
        </div>
        @endif
        
        <div class="card mb-4">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0">管理员</h5>
                <span class="badge bg-primary">{{ $organization->admins->count() }}</span>
            </div>
            <div class="card-body">
                @if($organization->admins->count() > 0)
                    <ul class="list-group list-group-flush">
                        @foreach($organization->admins as $admin)
                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                <div>
                                    <i class="fas fa-user-shield me-2 text-primary"></i>
                                    <span>{{ $admin->name }}</span>
                                    <small class="d-block text-muted">{{ $admin->email }}</small>
                                    @if($admin->roles->count() > 0)
                                        <div class="mt-1">
                                            @foreach($admin->roles as $role)
                                                <span class="badge bg-secondary me-1">{{ $role->name }}</span>
                                            @endforeach
                                        </div>
                                    @endif
                                </div>
                                @can('manage-organization-users')
                                <form action="{{ route('admin.organizations.remove-user', [$organization->id, $admin->id]) }}" method="POST" class="d-inline">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('确定要移除此管理员吗？')">
                                        <i class="fas fa-times"></i>
                                    </button>
                                </form>
                                @endcan
                            </li>
                        @endforeach
                    </ul>
                @else
                    <p class="text-center text-muted my-3">暂无管理员</p>
                @endif
                
                @can('manage-organization-users')
                <div class="mt-3">
                    <button type="button" class="btn btn-sm btn-outline-primary w-100" data-bs-toggle="modal" data-bs-target="#addAdminModal">
                        <i class="fas fa-plus me-1"></i>添加管理员
                    </button>
                </div>
                @endcan
            </div>
        </div>
        
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0">普通成员</h5>
                <span class="badge bg-primary">{{ $organization->users->count() - $organization->admins->count() }}</span>
            </div>
            <div class="card-body">
                @if($organization->users->count() > $organization->admins->count())
                    <ul class="list-group list-group-flush">
                        @foreach($organization->users as $user)
                            @if(!$organization->admins->contains($user->id))
                                <li class="list-group-item d-flex justify-content-between align-items-center">
                                    <div>
                                        <i class="fas fa-user me-2 text-secondary"></i>
                                        <span>{{ $user->name }}</span>
                                        <small class="d-block text-muted">{{ $user->email }}</small>
                                        @if($user->roles->count() > 0)
                                            <div class="mt-1">
                                                @foreach($user->roles as $role)
                                                    <span class="badge bg-secondary me-1">{{ $role->name }}</span>
                                                @endforeach
                                            </div>
                                        @endif
                                    </div>
                                    <div>
                                        @can('manage-organization-users')
                                        <button type="button" class="btn btn-sm btn-outline-primary me-1" onclick="promoteToAdmin({{ $user->id }})">
                                            <i class="fas fa-arrow-up"></i>
                                        </button>
                                        
                                        <form action="{{ route('admin.organizations.remove-user', [$organization->id, $user->id]) }}" method="POST" class="d-inline">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('确定要移除此成员吗？')">
                                                <i class="fas fa-times"></i>
                                            </button>
                                        </form>
                                        @endcan
                                    </div>
                                </li>
                            @endif
                        @endforeach
                    </ul>
                @else
                    <p class="text-center text-muted my-3">暂无普通成员</p>
                @endif
                
                @can('manage-organization-users')
                <div class="mt-3">
                    <button type="button" class="btn btn-sm btn-outline-primary w-100" data-bs-toggle="modal" data-bs-target="#addMemberModal">
                        <i class="fas fa-plus me-1"></i>添加成员
                    </button>
                </div>
                @endcan
            </div>
        </div>
    </div>
</div>

@can('manage-organization-users')
<!-- 添加管理员模态框 -->
<div class="modal fade" id="addAdminModal" tabindex="-1" aria-labelledby="addAdminModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="{{ route('admin.organizations.add-admin', $organization) }}" method="POST">
                @csrf
                <div class="modal-header">
                    <h5 class="modal-title" id="addAdminModalLabel">添加管理员</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="admin_user_id" class="form-label">选择用户</label>
                        <select class="form-select" id="admin_user_id" name="user_id" required>
                            <option value="">-- 选择用户 --</option>
                            @foreach(\App\Models\User::with('organizations')->whereNotIn('id', $organization->users->pluck('id'))->get() as $user)
                                <option value="{{ $user->id }}">
                                    {{ $user->name }} ({{ $user->email }})
                                    @if($user->organizations->count() > 0)
                                        - {{ $user->organizations->pluck('name')->join(', ') }}
                                    @endif
                                </option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">添加</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 添加成员模态框 -->
<div class="modal fade" id="addMemberModal" tabindex="-1" aria-labelledby="addMemberModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="{{ route('admin.organizations.add-member', $organization) }}" method="POST">
                @csrf
                <div class="modal-header">
                    <h5 class="modal-title" id="addMemberModalLabel">添加成员</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="member_user_id" class="form-label">选择用户</label>
                        <select class="form-select" id="member_user_id" name="user_id" required>
                            <option value="">-- 选择用户 --</option>
                            @foreach(\App\Models\User::with('organizations')->whereNotIn('id', $organization->users->pluck('id'))->get() as $user)
                                <option value="{{ $user->id }}">
                                    {{ $user->name }} ({{ $user->email }})
                                    @if($user->organizations->count() > 0)
                                        - {{ $user->organizations->pluck('name')->join(', ') }}
                                    @endif
                                </option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">添加</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 提升为管理员的表单 -->
<form id="promoteForm" action="{{ route('admin.organizations.promote-to-admin', $organization) }}" method="POST" style="display: none;">
    @csrf
    <input type="hidden" name="user_id" id="promoteUserId">
</form>

<script>
    function promoteToAdmin(userId) {
        if (confirm('确定要将此成员提升为管理员吗？')) {
            document.getElementById('promoteUserId').value = userId;
            document.getElementById('promoteForm').submit();
        }
    }
</script>
@endcan

@endsection